/**
 * 本地存储工具函数
 */

const PREFIX = 'reservation_';

/**
 * 存储数据到localStorage
 * @param key 键名
 * @param value 要存储的值
 */
export const setLocalStorage = (key: string, value: any): void => {
  try {
    const prefixedKey = PREFIX + key;
    if (typeof value === 'object') {
      localStorage.setItem(prefixedKey, JSON.stringify(value));
    } else {
      localStorage.setItem(prefixedKey, String(value));
    }
  } catch (error) {
    console.error('localStorage存储失败:', error);
  }
};

/**
 * 从localStorage获取数据
 * @param key 键名
 * @param defaultValue 默认值，当取不到值时返回
 * @returns 存储的值或默认值
 */
export const getLocalStorage = <T>(key: string, defaultValue?: T): T | null | undefined => {
  try {
    const prefixedKey = PREFIX + key;
    const value = localStorage.getItem(prefixedKey);
    
    if (value === null) {
      return defaultValue;
    }
    
    try {
      // 尝试解析JSON
      return JSON.parse(value);
    } catch (e) {
      // 如果不是JSON格式，直接返回字符串
      return value as unknown as T;
    }
  } catch (error) {
    console.error('localStorage读取失败:', error);
    return defaultValue;
  }
};

/**
 * 从localStorage删除数据
 * @param key 键名
 */
export const removeLocalStorage = (key: string): void => {
  try {
    const prefixedKey = PREFIX + key;
    localStorage.removeItem(prefixedKey);
  } catch (error) {
    console.error('localStorage删除失败:', error);
  }
};

/**
 * 清除所有以指定前缀开头的localStorage数据
 */
export const clearLocalStorage = (): void => {
  try {
    for (let i = 0; i < localStorage.length; i++) {
      const key = localStorage.key(i);
      if (key && key.startsWith(PREFIX)) {
        localStorage.removeItem(key);
      }
    }
  } catch (error) {
    console.error('localStorage清除失败:', error);
  }
};

/**
 * 存储数据到sessionStorage
 * @param key 键名
 * @param value 要存储的值
 */
export const setSessionStorage = (key: string, value: any): void => {
  try {
    const prefixedKey = PREFIX + key;
    if (typeof value === 'object') {
      sessionStorage.setItem(prefixedKey, JSON.stringify(value));
    } else {
      sessionStorage.setItem(prefixedKey, String(value));
    }
  } catch (error) {
    console.error('sessionStorage存储失败:', error);
  }
};

/**
 * 从sessionStorage获取数据
 * @param key 键名
 * @param defaultValue 默认值，当取不到值时返回
 * @returns 存储的值或默认值
 */
export const getSessionStorage = <T>(key: string, defaultValue?: T): T | null | undefined => {
  try {
    const prefixedKey = PREFIX + key;
    const value = sessionStorage.getItem(prefixedKey);
    
    if (value === null) {
      return defaultValue;
    }
    
    try {
      // 尝试解析JSON
      return JSON.parse(value);
    } catch (e) {
      // 如果不是JSON格式，直接返回字符串
      return value as unknown as T;
    }
  } catch (error) {
    console.error('sessionStorage读取失败:', error);
    return defaultValue;
  }
};

/**
 * 从sessionStorage删除数据
 * @param key 键名
 */
export const removeSessionStorage = (key: string): void => {
  try {
    const prefixedKey = PREFIX + key;
    sessionStorage.removeItem(prefixedKey);
  } catch (error) {
    console.error('sessionStorage删除失败:', error);
  }
};

/**
 * 清除所有以指定前缀开头的sessionStorage数据
 */
export const clearSessionStorage = (): void => {
  try {
    for (let i = 0; i < sessionStorage.length; i++) {
      const key = sessionStorage.key(i);
      if (key && key.startsWith(PREFIX)) {
        sessionStorage.removeItem(key);
      }
    }
  } catch (error) {
    console.error('sessionStorage清除失败:', error);
  }
}; 